import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test_demo/model/index.dart';

import 'bloc.dart';
import 'event.dart';
import 'state.dart';

class OrderEventBlocPage extends StatelessWidget {
  OrderEventBlocPage({super.key});

  TextStyle textStyle14 = const TextStyle(fontSize: 14);
  TextStyle textStyle16 = const TextStyle(fontSize: 16);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => OrderEventBlocBloc()..add(InitEvent()),
      child: Builder(builder: (context) => _buildPage(context)),
    );
  }

  Widget _buildPage(BuildContext context) {
    final bloc = BlocProvider.of<OrderEventBlocBloc>(context);

    return Scaffold(
      appBar: AppBar(
        title: const Text('订单'),
        centerTitle: true,
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Column(
        children: [
          BlocBuilder<OrderEventBlocBloc, OrderEventBlocState>(
            builder: (context, state) {
              return _buildBodyWidget(bloc);
            },
          ),
          _buildBottomWidget(bloc)
        ],
      ),
    );
  }

  Widget _buildBodyWidget(OrderEventBlocBloc cubit) {
    if(cubit.state.realOrderList.isEmpty) {
      return const Expanded(child: Center(child: Text('暂无数据'),));
    }
    return Expanded(
      child: BlocBuilder<OrderEventBlocBloc, OrderEventBlocState>(
        builder: (context, state) {
          return ListView.separated(
            itemBuilder: (context, index){
              OrderListModel model = state.realOrderList[index];
              return _buildOrderInfoWidget(model);
            },
            separatorBuilder: (context, index){
              return const SizedBox(height: 12,);
            },
            itemCount: state.realOrderList.length,
            shrinkWrap: true,
            padding: const EdgeInsets.all(12),
          );
        },
      ),
    );
  }

  Widget _buildOrderInfoWidget(OrderListModel model) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6),
          color: Colors.white
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(
                child: Text('订单编号：${model.orderDoc ?? ''}', style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),),
              ),
              Text(model.payWay ?? '', style: textStyle16,),
            ],
          ),
          _buildSizedBoxWidget(),
          ListView.separated(
            itemBuilder: (context, index) {
              OrderItemList? data = model.itemList?[index];
              return _buildItemInfoWidget(data);
            },
            separatorBuilder: (context, index) {
              return Container(height: 12,);
            },
            itemCount: model.itemList?.length ?? 0,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
          ),
          _buildSizedBoxWidget(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(model.orderTime ?? '', style: textStyle16,),
              Text('共计：${model.totalPrice ?? 0}', style: textStyle16,),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildItemInfoWidget(OrderItemList? data) {
    return Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6),
          color: const Color(0xFFF5F6F7)
      ),
      padding: const EdgeInsets.all(12),
      child: Row(
        children: [
          Container(
            color: const Color(0xFFdddddd),
            width: 80,
            height: 80,
            child: const Icon(Icons.image),
          ),
          const SizedBox(width: 12,),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(data?.orderGoodsName ?? '', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),),
                _buildSizedBoxWidget(),
                Text("商品编码：${data?.orderGoodsCode ?? ''}", style: textStyle14,),
                _buildSizedBoxWidget(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text("数量：${data?.orderGoodsQty ?? 0}", style: textStyle14,),
                    const SizedBox(width: 12,),
                    Text("价格：${data?.orderGoodsPrice ?? 0}", style: textStyle14,),
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBottomWidget(OrderEventBlocBloc cubit) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      color: Colors.white,
      child: BlocBuilder<OrderEventBlocBloc, OrderEventBlocState>(
        builder: (context, state) {
          return Row(
            children: [
              Text('当日成交单数：${cubit.orderCountOrPrice(type: 1)}', style: textStyle14,),
              Expanded(child: Text('当日成交金额：￥${cubit.orderCountOrPrice(type: 2)}', style: textStyle14, textAlign: TextAlign.end,))
            ],
          );
        },
      ),
    );
  }

  Widget _buildSizedBoxWidget() {
    return const SizedBox(height: 12);
  }

}

